//speech_server的测试客户端实现
#include "etcd.hpp"
#include "channel.hpp"
#include <gflags/gflags.h>
#include <thread>
#include "aip-cpp-sdk-4.16.7/speech.h"
#include "speech.pb.h"

//日志类参数
DEFINE_bool(log_mod,false,"true为发布模式,false为调试模式");
DEFINE_string(log_file,"","发布模式下日志存储的文件");
DEFINE_int32(log_level,0,"要输出的日志等级");

DEFINE_string(etcd_host,"http://127.0.0.1:2379","服务注册中心地址");
DEFINE_string(basedir,"/service","服务查询的根目录");
DEFINE_string(speech_service,"/service/speech_service","当前实例名称");
int main(int argc,char* argv[])
{
    google::ParseCommandLineFlags(&argc,&argv,true);
    zzy_mims::init_logger(FLAGS_log_mod,FLAGS_log_file,(spdlog::level::level_enum)FLAGS_log_level);
    //关闭brpc的默认日志输出
    logging::LoggingSettings logset;
    logset.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(logset);
    //1.创建RPC信道管理对象
    auto scm = std::make_shared<zzy_mims::ServChanManager>();
    scm->Declared(FLAGS_speech_service);
    //2.构建服务发现对象
    auto online = std::bind(&zzy_mims::ServChanManager::ServiceOnline,scm.get(),std::placeholders::_1,std::placeholders::_2);
    auto offline = std::bind(&zzy_mims::ServChanManager::ServiceOffline,scm.get(),std::placeholders::_1,std::placeholders::_2);
    std::shared_ptr<zzy_mims::Discovery> dclient = std::make_shared<zzy_mims::Discovery>(FLAGS_etcd_host,FLAGS_basedir,online,offline); 
   //获取信道
    auto channel = scm->choose(FLAGS_speech_service);
    if(channel)
    {
        //读取语音文件数据
        std::string file_content;
        aip::get_file_content("16k.pcm", &file_content);
        std::cout << file_content.size() << std::endl;
        // 3.进行RPC调用
        ::zzy_mims::SpeechService_Stub stub(channel.get());
        ::zzy_mims::SpeechRecognitionReq  req;
        req.set_speech_content(file_content);
        req.set_request_id("1314");
        
         // 因为如果需要用到异步调用的时候，异步调用可能会直接对cntl和rep进行释放，所以不能简单定义到栈上
        brpc::Controller *cntl = new brpc::Controller();
        ::zzy_mims::SpeechRecognitionRsp *resp = new ::zzy_mims::SpeechRecognitionRsp();
         stub.SpeechRecognition(cntl,&req,resp,nullptr);//同步调用
        if(cntl->Failed()==true)
            std::cout<<"RPC调用失败"<<cntl->ErrorText()<<std::endl;
        else
            std::cout<<"收到响应:"<<resp->errmsg()<<std::endl; 
         delete cntl;
        delete resp;
    }
    std::this_thread::sleep_for(chrono::seconds(2));

}